package jlb.andriva;

import jlb.andriva.wrappers.MainMenu;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class PagerActivity extends BaseActivity {
	
	/**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * three primary sections of the app. We use a {@link android.support.v4.app.FragmentPagerAdapter}
     * derivative, which will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
	public static ViewPager mViewPager;
    private AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    private int currentMenuItemPos = -1;
	private static FragmentActivity mActivity;

	public PagerActivity() {
		super(R.string.maintitle);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSlidingMenu().setMode(SlidingMenu.LEFT);
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		
		//setContentView(R.layout.content_frame);
		setContentView(R.layout.activity_pager);
		
		mActivity = this;
		
		// Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
        
        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                // Tab.
                //actionBar.setSelectedNavigationItem(position);
            	currentMenuItemPos = position;
            	ListView lv =((MainMenu)((BaseActivity)mActivity).mFrag).getListView();
        		View row = lv.getChildAt(position);
        		lv.performItemClick(row, position, position);
            	switch (position) {
				case 0:
					getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
					break;
				default:
					getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
					break;
				}
            }
        });
	}
	
	@Override
	public void onStart(){
		super.onStart();
		
		MainMenu menu = ((MainMenu)((BaseActivity)mActivity).mFrag);
        menu.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener(){

        	private View row;
			@Override
			public void onItemClick(AdapterView<?> lv, View v, int position, long id) {				
				if(!v.isSelected()){
					v.setSelected(true);
					if (row != null) {
				        row.setBackgroundResource(android.R.color.darker_gray);
				    }
				    else{
				    	for(int i=0;i<((ListView)lv).getChildCount();i++){
				    		((ListView)lv).getChildAt(i).setBackgroundResource(android.R.color.darker_gray);
				    	}
				    }
				    row = v;
				    v.setBackgroundResource(R.color.holo_blue_light);
					mViewPager.setCurrentItem(position);
				}
			}
        });
        
        getSlidingMenu().setOnOpenListener(new SlidingMenu.OnOpenListener() {
			
			@Override
			public void onOpen() {
				if(currentMenuItemPos == -1){
					ListView lv =((MainMenu)((BaseActivity)mActivity).mFrag).getListView();
					View row = lv.getChildAt(0);
					lv.performItemClick(row, 0, 0);
				}
			}
		});
	}

	/**
     * A {@link android.support.v4.app.FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    String data = mActivity.getIntent().getStringExtra("data");
                    Bundle bundle=new Bundle();
                    bundle.putString("data", data);
                    HomeActivity home = new HomeActivity();
                    home.setArguments(bundle);
                    return home;
                case 1: return new ConsumptionActivity();
                case 2: return new UpcomingClosuresActivity();
                case 3: return new LimitsActivity();
                case 4: return new RatesActivity();
                default: return null;
            }
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch(position){
                case 0: return mActivity.getString(R.string.home_title);
                case 1: return mActivity.getString(R.string.consumptions_title);
                case 2: return mActivity.getString(R.string.closures_title);
                case 3: return mActivity.getString(R.string.limits_title);
                case 4: return mActivity.getString(R.string.rates_title);
                default: return "";
            }
        }
    }
}
